class sceneClass {
    constructor(game){
        this.game = game
        this.elements = []
        this.enemy = []
        this.bullet = []
        this.bomb = []
    }

    static new (game){
        var i = new this(game)
        return i
    }

    addArray(img, array){
        img.scene = this
        array.push(img)
    }

    update (){
        /* 更新元素 */
        for (var e of this.elements){
            e.update()
        }

        /* 更新子弹 */
        for (var b of this.bullet){
            b.update()
        }

        /* 更新敌机 */
        for (var e of this.enemy){
            e.update()
        }

        // 更新爆炸
        for (var b of this.bomb){
            b.update()
        }
    }
    draw(){
        /* 绘制其他元素 */
        for (var e of this.elements){
            this.game.drawImage(e)
        }
        /* 绘制子弹 */
        for (var b of this.bullet){
            this.game.drawImage(b)
        }
        // 绘制敌机
        for (var e of this.enemy){
            this.game.drawImage(e)
        }
    }
}

class Scene extends sceneClass {
    constructor (game){
        super(game)
        this.setup()
    }         

    setup (){
        var s = this
        var esNo = 8 
        var cNo = 3

        // 加入背景
        s.bg = Bg.new(s.game, "bg")
        s.addArray(s.bg, s.elements)

        // 加入玩家
        s.player = Player.new(s.game, "player")
        s.addArray(s.player, s.elements)

        // 加入云
        for (var i = 0; i < cNo; i++){
            var c = Cloud.new(s.game, "cloud")
            s.addArray(c, s.elements)
        }

        // 加入敌机
        for (var i = 0; i < esNo; i++){
            var name = "enemy" + randomBetween(1,6)
            var e = Enemy.new(s.game, name)
            s.addArray(e, s.enemy)
        }


        this.game.registerActions("KeyA", function(){
            s.player.moveLeft()
        })
        this.game.registerActions("KeyD", function(){
            s.player.moveRight()
        })
        this.game.registerActions("KeyW", function(){
            s.player.moveUp()
        })
        this.game.registerActions("KeyS", function(){
            s.player.moveDown()
        })
        this.game.registerActions("KeyJ", function(){
            s.player.fire()
        })
    }
    
} 